﻿using KineBall.GameLogic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace KineBall.Rendering
{
    public enum Messages
    {
        CountDownOne,
        CountDownTwo,
        CountDownThree,
        Starting,
        BluePoint,
        RedPoint,
        Win,
        Lose,
        Pause
    }

    public class MessagesRendering
    {
        public Vector2 ScreenCenter { get; set; }
        public Texture2D[] Textures
        {
            get
            {
                return _textures;
            }

            set
            {
                _textures = value;
                calcoulateCenters();
            }
        }

        private Vector2[] texturesCenters;
        private Texture2D[] _textures;

        public MessagesRendering(Texture2D[] textures, Vector2 screenCenter)
        {
            ScreenCenter = screenCenter;
            texturesCenters = new Vector2[textures.Length];
            Textures = textures;
        }

        private void calcoulateCenters()
        {
            for (int i = 0; i < Textures.Length; i++)
            {
                texturesCenters[i] = new Vector2(Textures[i].Width * 0.5f, Textures[i].Height * 0.5f);
            }
        }

        public void DrawCountDown(GameMessageState state, SpriteBatch spriteBatch)
        {
            Vector2 messagePosition = ScreenCenter + state.posFromScreenCenter;

            float scale = state.scale;

            spriteBatch.Begin();
            switch (state.countDown)
            {
                case 1:
                    spriteBatch.Draw(Textures[(uint)Messages.CountDownOne], messagePosition, null, Color.White, 0.0f, texturesCenters[(uint)Messages.CountDownOne], scale, SpriteEffects.None, 0f);
                    break;
                case 2:
                    spriteBatch.Draw(Textures[(uint)Messages.CountDownTwo], messagePosition, null, Color.White, 0.0f, texturesCenters[(uint)Messages.CountDownTwo], scale, SpriteEffects.None, 0f);
                    break;
                case 3:
                    spriteBatch.Draw(Textures[(uint)Messages.CountDownThree], messagePosition, null, Color.White, 0.0f, texturesCenters[(uint)Messages.CountDownThree], scale, SpriteEffects.None, 0f);
                    break;
            }
            spriteBatch.End();
        }

        public void DrawStarting(GameMessageState state, SpriteBatch spriteBatch)
        {
            Vector2 messagePosition = ScreenCenter + state.posFromScreenCenter;

            float scale = state.scale;

            spriteBatch.Begin();
            spriteBatch.Draw(Textures[(uint)Messages.Starting], messagePosition, null, state.color, 0.0f, texturesCenters[(uint)Messages.Starting], scale, SpriteEffects.None, 0f);
            spriteBatch.End();
        }

        public void DrawPoint(GameMessageState state, SpriteBatch spriteBatch, bool playerPoint)
        {
            Vector2 messagePosition = ScreenCenter + state.posFromScreenCenter;

            float scale = state.scale;

            spriteBatch.Begin();

            if (playerPoint)
                spriteBatch.Draw(Textures[(uint)Messages.RedPoint], messagePosition, null, state.color, 0.0f, texturesCenters[(uint)Messages.RedPoint], scale, SpriteEffects.None, 0f);
            else
                spriteBatch.Draw(Textures[(uint)Messages.BluePoint], messagePosition, null, state.color, 0.0f, texturesCenters[(uint)Messages.BluePoint], scale, SpriteEffects.None, 0f);

            spriteBatch.End();
        }

        public void DrawLoseWin(GameMessageState state, SpriteBatch spriteBatch, bool win)
        {
            Vector2 messagePosition = ScreenCenter + state.posFromScreenCenter;

            float scale = state.scale;

            spriteBatch.Begin();

            if (win)
                spriteBatch.Draw(Textures[(uint)Messages.Win], messagePosition, null, state.color, 0.0f, texturesCenters[(uint)Messages.Win], scale, SpriteEffects.None, 0f);
            else
                spriteBatch.Draw(Textures[(uint)Messages.Lose], messagePosition, null, state.color, 0.0f, texturesCenters[(uint)Messages.Lose], scale, SpriteEffects.None, 0f);

            spriteBatch.End();
        }

        public void DrawPauseMessage(GameMessageState state, SpriteBatch spriteBatch)
        {
            Vector2 messagePosition = Vector2.Zero;

            float scale = state.scale;

            spriteBatch.Begin();

            spriteBatch.Draw(Textures[(uint)Messages.Pause], Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            
            spriteBatch.End();
        }
    }
}
